package com.qf.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * projectName: shop-manage
 *
 * @author: 元
 * time: 2021/9/16 0016 9:14
 * description:商品类目表实现类
 */
@Service
public class CategoryServiceImpl implements CategoryService{

    @Autowired
    private DtsCategoryMapper dtsCategoryMapper;

    @Override
    public IPage<DtsCategory> findByPage(Integer page, Integer limit, Integer id, String name, String sort, String order) {
        //分页显示数据
        Page<DtsCategory> categoryPage = new Page<>(page,limit);
        //查询数据
        QueryWrapper queryWrapper = new QueryWrapper();
        if (id !=null){
            queryWrapper.like("id",id);
        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if ("asc".equalsIgnoreCase(order)){
            queryWrapper.orderByAsc(sort);
        }else{
            queryWrapper.orderByAsc(sort);
        }

        IPage<DtsCategory> resultPage = dtsCategoryMapper.selectPage(categoryPage,queryWrapper);

        return resultPage;
    }

    @Override
    public List<Map> findLevel1() {
        List<Map> mapList = dtsCategoryMapper.selectLevel1();
        return mapList;
    }

    @Override
    public boolean checkOutCategoryName(String name) {
        int rows = dtsCategoryMapper.queryCategoryName(name);
        return rows>=1;
    }

    @Override
    public DtsCategory saveCategory(DtsCategory dtsCategory) {
        dtsCategory.setAddTime(new Date());
        dtsCategory.setDeleted(false);
        dtsCategoryMapper.insert(dtsCategory);
        return dtsCategory;
    }

    @Override
    public int updateCategory(DtsCategory dtsCategory) {
        dtsCategory.setUpdateTime(new Date());
        int count = dtsCategoryMapper.updateById(dtsCategory);
        return count;
    }

    @Override
    public int removeById(Integer id) {
        int count = dtsCategoryMapper.deleteById(id);
        return count;
    }

    @Override
    public List<DtsCategory> selectLI() {
        QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level","L1");
        queryWrapper.eq("deleted",0);
        return dtsCategoryMapper.selectList(queryWrapper);
    }



    @Override
    public List<DtsCategory> selectById(Integer id) {
        QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",id);
        queryWrapper.eq("deleted",0);
        List<DtsCategory> list = dtsCategoryMapper.selectList(queryWrapper);
        return list;

    }


}
